import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vite'
import { resolve } from 'path'
import { visualizer } from 'rollup-plugin-visualizer'
import {
  VantResolver,
  ElementPlusResolver
} from 'unplugin-vue-components/resolvers'
import { rollupInput } from './buildRollupInput'
import { deletePages } from './rollupPlugin/end'
import legacy from '@vitejs/plugin-legacy'
import topLevelAwait from 'vite-plugin-top-level-await'

import AutoImport from 'unplugin-auto-import/vite'
import postcsspxtorem from 'postcss-pxtorem' // vant适配
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const input = rollupInput(mode)
  const plugins = [
    legacy({
      targets: ['chrome 52', 'android 4.4'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime']
    }),
    topLevelAwait(),
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [VantResolver(), ElementPlusResolver()]
    }),
    deletePages()
  ]

  if (mode === 'beta') {
    plugins.push(
      visualizer({
        open: false
      })
    )
  }
  return {
    plugins,
    root: 'src',
    base: './',
    publicDir: 'public',
    server: {
      host: true,
      port: 8082,
      proxy: {
        '/apis': {
          target: 'https://hwvirtualhumantest.59wanmei.com',
          // target: 'https://saastest.17wanxiao.com',
          // target: 'http://virtualhuman2.59wanmei.com',
          // target: 'http://192.168.124.141:12888',
          // target: 'http://192.168.121.93:8090', // cuibin
          // target: 'http://192.168.121.78', // gongyifan
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/apis/, '')
        },
        '/digitPeopleUploadApi': {
          target: 'https://hwvirtualhumantest.59wanmei.com:1888/api',
          // target: 'http://virtualhuman.59wanmei.com',
          // target: 'http://192.168.121.78',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/digitPeopleUploadApi/, '')
        }
      }
    },
    build: {
      outDir: resolve(__dirname, 'dist'),
      rollupOptions: {
        input
      },
      target: 'es2015',
      // sourcemap: mode !== 'prod'
      sourcemap: false
    },
    css: {
      postcss: {
        plugins: [
          postcsspxtorem({
            rootValue: 52,
            propList: ['*']
          })
        ]
      }
    }
  }
})
